package net.awpspace.mobile.management.book;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.form.BaseForm;
import net.awpspace.mobile.global.Client;
import net.awpspace.mobile.global.Global;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;

public class ViewInsert extends BaseForm {

	private BookModel model;

	private Command cmdDone;

	private TextField txtName;
	private TextField txtAuthor;
	private TextField txtImage;
	private TextField txtDescription;

	public ViewInsert(MIDlet app, BaseForm parent, BookModel model) {
		super(app, parent);
		this.model = model;
		setTitle(model == null ? Global.BOOK_INSERT : Global.BOOK_EDIT);

		cmdDone = new Command("Done");
		txtName = new TextField(25);
		if (model != null) {
			txtName.setText(model.getName());
		}
		txtAuthor = new TextField(25);
		if (model != null) {
			txtAuthor.setText(model.getAuthor());
		}
		txtImage = new TextField(25);
		if (model != null) {
			txtImage.setText(model.getImage());
		}
		txtDescription = new TextField(25);
		if (model != null) {
			txtDescription.setText(model.getDescription());
		}

		addComponent(new Label("Name"));
		addComponent(txtName);
		addComponent(new Label("Author"));
		addComponent(txtAuthor);
		addComponent(new Label("Image Link"));
		addComponent(txtImage);
		addComponent(new Label("Description"));
		addComponent(txtDescription);
		addComponent(new Button(cmdDone));
	}

	protected void processCommand(ActionEvent e) {
		if (e.getCommand().equals(cmdDone)) {
			Client.getInstance().update(
					new BookModel(model == null ? -1 : model.getId(), txtName
							.getText(), txtAuthor.getText(),
							txtImage.getText(), txtDescription.getText()));
		}
	}
}